Passed
Push — feature/netlify-search ( 939dfd...fbe251 )
by Kevin Van
05:37
created

MatchTeaser.tsx ➔ getData   B

Complexity

Conditions 6

Size

Total Lines 5
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 5
rs 8.6666
c 0
b 0
f 0
cc 6
1
import React, { FunctionComponent, useState, useEffect, Fragment } from "react"
2
import { graphql, useStaticQuery } from "gatsby"
3
4
import axios from "axios"
5
import LazyLoad from "react-lazyload"
6
import classNames from "classnames"
7
import Moment from "moment-timezone"
8
import "moment/locale/nl-be"
9
10
import { mapPsdStatus } from "../scripts/helper"
11
12
import "./MatchTeaser.scss"
13
import MiniRanking from "./MiniRanking"
14
15
export const MatchTeaserDetail: FunctionComponent<MatchTeaserDetailProps> = ({
16
  match,
17
  includeRankings = false,
18
}: MatchTeaserDetailProps) => {
19
  Moment.locale(`nl-BE`)
20
  const d = Moment.tz(match.date, `Europe/Brussels`)
21
  const matchPlayed = (match.status === 0 && match.goalsHomeTeam !== null && match.goalsAwayTeam !== null) || false
22
23
  return (
24
    <article className="match__teaser">
25
      <header>
26
        <h3>{match.teamName.replace(`Voetbal : `, ``)}</h3>
27
        <div>
28
          {match.status !== 0 && (
29
            <Fragment>
30
              <time
31
                className="match__teaser__datetime match__teaser__datetime--date"
32
                dateTime={d.format(`YYYY-MM-DD - H:mm`)}
33
              >
34
                {d.format(`dddd DD MMMM - H:mm`)}
35
              </time>
36
              <span className="match__teaser__datetime match__teaser__datetime--status">
37
                {mapPsdStatus(match.status)}
38
              </span>
39
            </Fragment>
40
          )}
41
          {match.status === 0 && (
42
            <Fragment>
43
              <time className="match__teaser__datetime match__teaser__datetime--date" dateTime={d.format(`YYYY-MM-DD`)}>
44
                {d.format(`dddd DD MMMM`)}
45
              </time>
46
              <time className="match__teaser__datetime match__teaser__datetime--time" dateTime={d.format(`H:mm`)}>
47
                {d.format(`H:mm`)}
48
              </time>
49
            </Fragment>
50
          )}
51
        </div>
52
      </header>
53
      <main>
54
        <div
55
          className={classNames(`match__teaser__team`, `match__teaser__team--home`, {
56
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam > match.goalsAwayTeam,
57
          })}
58
        >
59
          <LazyLoad debounce={false}>
60
            <img
61
              src={match.homeClub.logo}
62
              alt={match.homeClub.name}
63
              className="match__teaser__logo match__teaser__logo--home"
64
            />
65
          </LazyLoad>
66
          {match.homeClub.name}
67
        </div>
68
69
        {matchPlayed || <span className="match__teaser__vs">vs</span>}
70
        {matchPlayed && (
71
          <span className="match__teaser__vs match__teaser__vs--score">
72
            {match.goalsHomeTeam} - {match.goalsAwayTeam}
73
          </span>
74
        )}
75
76
        <div
77
          className={classNames(`match__teaser__team`, `match__teaser__team--away`, {
78
            "match__teaser__team--winner": matchPlayed && match.goalsHomeTeam < match.goalsAwayTeam,
79
          })}
80
        >
81
          <LazyLoad debounce={false}>
82
            <img
83
              src={match.awayClub?.logo}
84
              alt={match.awayClub?.name}
85
              className="match__teaser__logo match__teaser__logo--away"
86
            />
87
          </LazyLoad>
88
          {match.awayClub?.name}
89
        </div>
90
      </main>
91
      {includeRankings && (
92
        <MiniRanking
93
          teamId={match.homeTeamId || match.awayTeamId}
94
          homeTeam={match.homeClub.name}
95
          awayTeam={match.awayClub?.name}
96
        />
97
      )}
98
    </article>
99
  )
100
}
101
102
export const MatchTeaser: FunctionComponent<MatchTeaserProps> = ({
103
  teamId,
104
  action,
105
  includeRankings = false,
106
}: MatchTeaserProps) => {
107
  if (action !== `prev` && action !== `next`) {
108
    throw new Error(`Invalid action provided`)
109
  }
110
111
  const [data, setData] = useState<Match[]>([])
112
113
  const {
114
    site: {
115
      siteMetadata: { kcvvPsdApi },
116
    },
117
  }: MatchesQueryData = useStaticQuery(graphql`
118
    {
119
      site {
120
        siteMetadata {
121
          kcvvPsdApi
122
        }
123
      }
124
    }
125
  `)
126
127
  useEffect(() => {
128
    async function getData() {
129
      const response = await axios.get(`${kcvvPsdApi}/matches/${action}`, {
130
        params: { include: teamId },
131
      })
132
      setData(response.data)
133
    }
134
    getData()
135
  }, [])
136
137
  if (data.length > 0) {
138
    return <MatchTeaserDetail match={data[0]} includeRankings={includeRankings} />
139
  } else {
140
    return <div className="match__teaser__no_match">Geen wedstrijd gevonden</div>
141
  }
142
}
143
144
export const MatchTeasers: FunctionComponent<MatchTeasersProps> = ({
145
  teamId,
146
  includeRankings = false,
147
}: MatchTeasersProps) => (
148
  <div className="match__teasers">
149
    <MatchTeaser teamId={teamId} action="prev" includeRankings={includeRankings} />
150
    <MatchTeaser teamId={teamId} action="next" includeRankings={includeRankings} />
151
  </div>
152
)
153